import Vue from "vue";
import Vuex from "vuex";
import user from "./modules/user";

Vue.use(Vuex);

export function createStore() {
  return new Vuex.Store({
    modules: {
      user
    },
    state: {
      name: ""
    },
    actions: {
      fetchItem({ commit }) {
        // `store.dispatch()` 会返回 Promise，
        // 以便我们能够知道数据在何时更新
        return new Promise(resolve => {
          commit("setName", "111");
          resolve();
        });
      }
    },
    mutations: {
      setName(state, name) {
        // state.name = name;
        Vue.set(state, "name", name);
      }
    }
  });
}
